//
//  EasyTakePicPublishViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/1.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EasyTakePicPublishViewModel.h"

#import "ProvincesListData.h"

@implementation EasyTakePicPublishSubmitData

- (id)init
{
    self = [super init];
    
    if (self)
    {
        DataManager* dm = [DataManager sharedDataManager];
        
        NSArray* addrArr = [dm.address componentsSeparatedByString:@"-"];
        NSString* cityName = addrArr[1];
        bool isFinded = NO;
        ProvinceListData* findPListData = nil;
        for (ProvinceListData* curPPListData in dm.addrData.datas)
        {
            for (ProvinceListData* curSPListData in curPPListData.datas)
            {
                if ([cityName isEqualToString:curSPListData.name])
                {
                    isFinded = YES;
                    
                    findPListData = curSPListData;
                    
                    break;
                }
            }
            
            if (isFinded)
                break;
        }
        
        self.areaID = findPListData ? findPListData.areaID : @"";
        
        self.typeID = @"1";
        
        self.content = @"";
    }
    
    return self;
}

@end

@interface EasyTakePicPublishViewModel ()

@property (nonatomic, strong) ProvinceListData* zoneData;

@end

@implementation EasyTakePicPublishViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    self.isShowCamera = NO;
    
    DataManager* dm = [DataManager sharedDataManager];
    
    self.submitData = [[EasyTakePicPublishSubmitData alloc] init];
    
    RAC(self, type) = [RACObserve(self.submitData, typeID) map:^id(id value)
    {
        NSInteger typeID = [value integerValue];
        
        return typeID==1 ? @"生态宣传" : @"污染曝光";
    }];
    
    RAC(self, content) = RACObserve(self.submitData, content);
    RAC(self, area) = [RACObserve(self.submitData, areaID) map:^id(id value)
    {
        for (ProvinceListData* curPPListData in dm.addrData.datas)
        {
            for (ProvinceListData* curSPListData in curPPListData.datas)
            {
                if ([value isEqualToString:curSPListData.areaID])
                {
                    return curSPListData.name;
                }
            }
        }
        
        return @"全国";
    }];
    
    self.uploadImgs = [NSMutableArray array];
    
    self.addImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        NSMutableArray* newUploadImgs = [NSMutableArray arrayWithArray:[self.uploadImgs arrayByAddingObjectsFromArray:input]];
        
        self.uploadImgs = newUploadImgs;
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
    
    self.deleteImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        self.uploadImgs = [NSMutableArray arrayWithArray:self.uploadImgs];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];

    self.submitCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        @weakify(self);
        return [[self checkSubmitSignal] then:^RACSignal *
        {
            @strongify(self);
            
            return [self requestSubmitSignal];
        }];
    }];
    
    RACSignal* zoneChangeSignal = [[RACObserve(self, zoneData) distinctUntilChanged] skip:1];
    [[zoneChangeSignal replay] subscribeNext:^(id x)
     {
         @strongify(self);
         
         ProvinceListData* curPListData = x;
         
         if (x == nil)
         {
             self.submitData.areaID = @"";
         }
         else
         {
             self.submitData.areaID = curPListData.areaID;
         }
     }];
}

- (void)resetZoneData:(ProvinceListData*)zoneData
{
    self.zoneData = zoneData;
}

- (RACSignal*)checkSubmitSignal
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.submitData.typeID isNotBlank])
    {
        result = @"请选择发布类型";
    }
    
    if (![result isNotBlank] && ![self.submitData.content isNotBlank])
    {
        result = @"请输入想说的话";
    }
    
    if (![result isNotBlank] && ![self.submitData.areaID isNotBlank])
    {
        result = @"请选择所在地";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

- (RACSignal*)requestSubmitSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        NSMutableArray* afuploadImgs = [NSMutableArray array];
        for (NSInteger index=0; index<self.uploadImgs.count; index++)
        {
            UIImage* curImg = self.uploadImgs[index];
            
            AFUploadImage* afUploadImg = [AFUploadImage createWithImage:curImg info:@{@"Name":@"Images"}];
            [afuploadImgs addObject:afUploadImg];
        }
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_release params:interface_param_release(um.userData.userID, self.submitData.typeID, self.submitData.content, afuploadImgs, @0, @0, self.submitData.areaID)] subscribeNext:^(id x)
        {
            @strongify(self);
            
            NSDictionary* result = x;
            
            [subscriber sendNext:result[SUCommandSysMessageKey]];
            [subscriber sendCompleted];
        } error:^(NSError *error)
        {
            //错误处理
            
            [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

@end
